package cn.edu.thu.tsquality.core.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by gaoyu on 2018/5/16 16:52.
 * E-mail address is nkugaoyu@163.com.
 * Copyright  2018 gaoyu. All Rights Reserved.
 *
 * @author gaoyu
 */
public class DateUtil {
  private static ThreadLocal<SimpleDateFormat> local = new ThreadLocal<SimpleDateFormat>();
  private static String formatString = "yyyy-MM-dd HH:mm:ss.SSS";

  public static void setDateFormat(String formatStr) {
    formatString = formatStr;
    SimpleDateFormat dateFormat = new SimpleDateFormat(formatString);
    local.set(dateFormat);
  }

  private static SimpleDateFormat getDateFormat() {
    SimpleDateFormat dateFormat = local.get();
    if (dateFormat == null) {
      dateFormat = new SimpleDateFormat(formatString);
      local.set(dateFormat);
    }
    return dateFormat;
  }

  public static String format(Date date) {
    return getDateFormat().format(date);
  }

  public static String format(long timestamp) {
    return getDateFormat().format(timestamp);
  }

  public static Date parse(String dateStr) throws ParseException {
    return getDateFormat().parse(dateStr);
  }


  public static long getTimestamp(String timeStr) throws ParseException {
    try {
      return parse(timeStr).getTime();
    } catch (ParseException e) {
//      e.printStackTrace();
      throw e;
    }
  }

  public static void main(String[] args) {
    System.out.println("hhh");
  }
}
